﻿using System;
using System.IO;
using System.Xml.Serialization;

namespace ResourceLocalization
{
    /// <summary>
    /// 语言 配置
    /// </summary>
    public class ResLanguageConfiguration
    {
        /// <summary>
        /// 保存 路径
        /// </summary>
        static string XmlFilePath = "ResLanguageConfiguration.xml";
       
        /// <summary>
        /// 设置的语言（已保存） 
        /// </summary>
        public ResLanguage SettingResLanguage { get; set; } = ResLanguage.Zh;

        /// <summary>
        /// 加载
        /// </summary>
        /// <returns></returns>
        public static ResLanguageConfiguration Load()
        {
            if (File.Exists(XmlFilePath))
            {
                try
                {
                    using (FileStream fileStream = new FileStream(XmlFilePath, FileMode.OpenOrCreate))
                    {
                        var hisConfiguration = new XmlSerializer(typeof(ResLanguageConfiguration)).Deserialize(fileStream) as ResLanguageConfiguration;
                        return hisConfiguration ?? new ResLanguageConfiguration();
                    }
                }
                catch (Exception)
                {
                    File.Delete(XmlFilePath);
                }
            }
            return new ResLanguageConfiguration();
        }
        /// <summary>
        /// /
        /// </summary>
        public void Save()
        {
            if (File.Exists(XmlFilePath))
            {
                File.Delete(XmlFilePath);
            }
            try
            {
                using (FileStream fileStream = new FileStream(XmlFilePath, FileMode.OpenOrCreate))
                {
                    new XmlSerializer(typeof(ResLanguageConfiguration)).Serialize(fileStream, this);
                }
            }
            catch (Exception)
            {
            }
        }
    }
}
